{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import math\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(X):\n",
    "    if(X>=0):\n",
    "        return 1.0/(1+np.exp(-X))\n",
    "    return np.exp(X)/(1+np.exp(X))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def upblg_2(txt,alpha=0.01,loop=500):\n",
    "    \n",
    "    (m,n)=txt.shape\n",
    "    \n",
    "    X=np.column_stack([txt[:,:-1],np.ones((m,1))])\n",
    "    \n",
    "    y=txt[:,-1].reshape(m,1)\n",
    "    \n",
    "    w=np.ones(X.shape[1])\n",
    "    \n",
    "    for j in range(loop):\n",
    "        indexs=(list(range(m)))\n",
    "        np.random.shuffle(indexs)\n",
    "        for i in range(m):\n",
    "            alpha=4/(1+j+i)+0.01\n",
    "            y_hat=sigmoid(X[indexs[i]].dot(w))\n",
    "            w=w+alpha*X[indexs[i]]*(y[indexs[i]]-y_hat)\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "train=np.loadtxt('horseColicTraining.txt')\n",
    "test=np.loadtxt('horseColicTest.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify(inX,weights):\n",
    "    prob=sigmoid(weights.T.dot(inX))\n",
    "    if prob>0.5:\n",
    "        return 1.0\n",
    "    else:\n",
    "        return 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2. ,  1. , 38.5, ...,  6.3,  0. ,  0. ],\n",
       "       [ 2. ,  1. , 37.6, ...,  6.3,  1. ,  5. ],\n",
       "       [ 1. ,  1. , 37.7, ..., 70. ,  3. ,  2. ],\n",
       "       ...,\n",
       "       [ 1. ,  1. , 38. , ..., 65. ,  3. ,  2. ],\n",
       "       [ 2. ,  1. , 38. , ...,  5.8,  0. ,  0. ],\n",
       "       [ 2. ,  1. , 37.6, ...,  6. ,  0. ,  0. ]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xTest=test[:,:-1]\n",
    "xTest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def colicTest(train,test,alpha=0.001,maxCycles=5000):\n",
    "    weights=upblg_2(train,alpha=alpha,loop=maxCycles)\n",
    "    \n",
    "    xTest=test[:,:-1]\n",
    "    result=[]\n",
    "    for inX in xTest:\n",
    "        label=classify(np.append(inX,1),weights)\n",
    "        result.append(label)\n",
    "    return np.sum(test[:,-1]==result)/test.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "67"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.40298507462686567"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colicTest(train,test,0.01,10000)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "774a858b74b9162cfeaabab2cd52460f9ad2aea53b492b6366c174bcba156af9"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
